const express = require(`express`);
const router = express.Router();
var FormData = require("form-data");
var request = require("request");
router.post("/api/v1/PhoneMessage", async (req, res) => {
  try {
    var randoms = Math.random()
      .toString()
      .substr(2, 4);
    console.log(randoms);
    console.log("本次验证码为" + randoms);

    var phone = req.body.phone;
    var requestData = {
      userid: "599",
      account: "zph_culture",
      password: "zph_cul123",
      mobile: phone,
      content: "本次验证码为" + randoms,
      action: "send"
    };
    console.log(requestData);

    let form = new FormData();
    form.append("userid", "599");
    form.append("account", "zph_culture");
    form.append("password", "zph_cul123");
    form.append("mobile", phone);
    form.append(
      "content",
      "验证码为" + randoms + "，您正在身份验证，若非本人操作，请勿泄露。"
    );
    form.append("action", "send");

    var request_url = "http://119.146.222.201:9199/smsProxy/sms.do";

    console.log("request_url ==> ", request_url);

    form.submit(request_url, function(err, data) {
      res.json({ code: "200", data: randoms });
    });
  } catch {
    res.json({ code: "201", data: "获取失败" });
  }
});
module.exports = router;
